package dao

import (
	"errors"
	"gitee.com/gopher2011/gva/server/util"
	"time"
)

// 这是 SysUser 的全局公开dao对象
var SysUserDao = sysUserDao{}

// 这是 sys_user表 的实体类
type SysUser struct {
	Id          int       `json:"id" form:"id" binding:"id"`
	CreatedAt   time.Time `json:"created_at" form:"created_at" binding:"created_at"`
	UpdatedAt   time.Time `json:"updated_at" form:"updated_at" binding:"updated_at"`
	DeletedAt   time.Time `json:"deleted_at" form:"deleted_at" binding:"deleted_at"`
	Uuid        string    `json:"uuid" form:"uuid" binding:"uuid"`                         // 用户UUID
	Username    string    `json:"username" form:"username" binding:"username"`             // 用户登录名
	Password    string    `json:"password" form:"password" binding:"password"`             // 用户登录密码
	NickName    string    `json:"nick_name" form:"nick_name" binding:"nick_name"`          // 用户昵称
	HeaderImg   string    `json:"header_img" form:"header_img" binding:"header_img"`       // 用户头像
	AuthorityId string    `json:"authority_id" form:"authority_id" binding:"authority_id"` // 用户角色ID
	SysAuthorityResp
}

type SysAuthorityResp struct {
	AuthorityId   string    `json:"a_authority_id" form:"authority_id" binding:"authority_id"`
	AuthorityName string    `json:"a_authority_name" form:"authority_name" binding:"authority_name"`
	ParentId      string    `json:"a_parent_id" form:"parent_id" binding:"parent_id"`
	DefaultRouter string    `json:"a_default_router" form:"default_router" binding:"default_router"`
	CreatedAt     time.Time `json:"a_created_at" form:"created_at" binding:"created_at"`
	UpdatedAt     time.Time `json:"a_updated_at" form:"updated_at" binding:"updated_at"`
	DeletedAt     time.Time `json:"a_deleted_at" form:"deleted_at" binding:"deleted_at"`
}


// 注意:这是 SysUser 的私有dao对象
type sysUserDao struct {
	Obj            SysUser
	Insert         func(arg SysUser) int                   //插入一条记录
	Update         func(arg SysUser) int                   //更新一条记录 (设置用户信息)
	Delete         func(id int) int                        `arg:"id"` //删除一条记录
	Select         func(id string) SysUser                 `arg:"id"` // 通过id获取用户信息
	login          func(username, password string) SysUser `arg:"username,password"`
	userExist      func(username string) SysUser           `arg:"username"`
	changePassword func(arg ChangePassword) int            // 修改用户密码
	list           func(arg Page) []SysUser                //模糊分页查询
	SetAuth        func(arg SetUserAuth) int               //  设置一个用户的权限
	FindUserByUuid func(uuid string) SysUser               `arg:"uuid"`
}

// 用户注册
func (it *sysUserDao)Register(u SysUser)error{
	user := it.userExist(u.Username)
	if user.Id >0 {
		return errors.New("用户名已注册")
	}
	rows := it.Insert(u)
	if rows <=0 {
		return errors.New("注册失败")
	}
	return nil
}

func (it *sysUserDao)Login(username, password string)(*SysUser,error){
	user := it.userExist(username)
	if user.Id <= 0 {
		return nil,errors.New("该用户未注册，请先注册")
	}
	login := it.login(username, password)
	if login.Id <=0 {
		return nil,errors.New("请输入正确的密码")
	}
	return &login,nil
}

// 修改用户密码
func (it *sysUserDao)ChangePassword(arg ChangePassword)error{
	arg.Password = util.Md5([]byte(arg.Password))
	arg.NewPassword = util.Md5([]byte(arg.NewPassword))
	if row := it.changePassword(arg);row <=0 {
		return errors.New("请输入正确的用户名和原始密码")
	}
	return nil
}

// 分页获取数据
func (it *sysUserDao)GetUserInfoList(arg Page)([]SysUser,int,error){
	arg.PageNum = (arg.PageNum - 1)*arg.PageSize
	array := it.list(arg)
	if len(array) == 0 {
		return nil,0,errors.New("分页查询 SysUser 失败")
	}
	return array,len(array),nil
}

// 通过id获取用户信息
//func (it *sysUserDao)FindUserById(id int) (error,SysUser) {
//	u := it.Select(id)
//	if u.Id <=0 {
//		return errors.New("通过id获取用户信息失败"),u
//	}else {
//		return nil,u
//	}
//}



